package com.kantboot.business.ac.service.impl;

import com.kantboot.business.ac.domain.entity.BusAcRechargeProduct;
import com.kantboot.business.ac.domain.entity.BusAcRechargeProductOrder;
import com.kantboot.business.ac.domain.entity.BusAcYrApiRechargeParam;
import com.kantboot.business.ac.domain.vo.BusAcRechargeProductPhoneTypeVO;
import com.kantboot.business.ac.domain.vo.BusAcRechargeProductTypeVO;
import com.kantboot.business.ac.repository.*;
import com.kantboot.business.ac.service.IBusAcRechargeProductService;
import com.kantboot.business.ac.service.IBusAcYrApiService;
import com.kantboot.functional.pay.order.module.entity.FunctionalPayOrder;
import com.kantboot.functional.pay.order.service.IFunctionalPayOrderService;
import com.kantboot.system.user.domain.entity.SysUser;
import com.kantboot.system.user.domain.entity.SysUserRole;
import com.kantboot.system.user.service.ISysUserService;
import com.kantboot.util.common.exception.BaseException;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BusAcRechargeProductServiceImpl implements IBusAcRechargeProductService {

    @Resource
    private BusAcRechargeProductTypeVORepository repository;

    @Resource
    private BusAcRechargeProductPhoneTypeVORepository phoneTypeVORepository;

    @Resource
    private IFunctionalPayOrderService functionalPayOrderService;

    @Resource
    private ISysUserService userService;

    @Resource
    private BusAcRechargeProductRepository productRepository;

    @Resource
    private IBusAcYrApiService acYrApiService;

    @Resource
    private BusAcRechargeProductOrderRepository orderRepository;

    @Resource
    private BusAcYrApiRechargeParamRepository apiRechargeParamRepository;

    @Override
    public List<BusAcRechargeProductTypeVO> getAll() {
        return repository.findAll();
    }

    @Override
    public List<BusAcRechargeProductPhoneTypeVO> getAllPhone() {
        return phoneTypeVORepository.findAll();
    }

    @Override
    public Double getAmountByProductId(Long productId, Long userId) {
        SysUser user = userService.getById(userId);
        List<SysUserRole> roleList = user.getRoleList();

        // 是否是高级合伙人
        boolean isSeniorPartner = false;
        // 是否是合伙人
        boolean isPartner = false;
        // 是否是vip
        boolean isVip = false;

        for (SysUserRole sysUserRole : roleList) {
            if ("seniorPartner".equals(sysUserRole.getRoleCode())) {
                isSeniorPartner = true;
            }
            if ("partner".equals(sysUserRole.getRoleCode())) {
                isPartner = true;
            }
            if ("vip".equals(sysUserRole.getRoleCode())) {
                isVip = true;
            }
        }

        // 根据产品id获取产品信息
        BusAcRechargeProduct busAcRechargeProduct = productRepository.findById(productId).orElseThrow(() ->
                BaseException.of("productNotFound", "产品不存在"));

        Double amount = null;

        if (isSeniorPartner) {
            // 高级合伙人的价格
            amount = busAcRechargeProduct.getSeniorPartnerPrice();
        } else if (isPartner) {
            // 合伙人的价格
            amount = busAcRechargeProduct.getPartnerPrice();
        } else if (isVip) {
            // vip的价格
            amount = busAcRechargeProduct.getVipUserPrice();
        } else {
            // 普通用户的价格
            amount = busAcRechargeProduct.getNormalUserPrice();
        }
        return amount;
    }

    @Override
    public BusAcRechargeProductOrder getOrderById(Long orderId) {
        return orderRepository.findByOrderId(orderId);
    }

    @Override
    public FunctionalPayOrder generatePayOrder(BusAcRechargeProductOrder order) {
        SysUser self = userService.getSelf();
        List<SysUserRole> roleList = self.getRoleList();
        // 是否是高级合伙人
        boolean isSeniorPartner = false;
        // 是否是合伙人
        boolean isPartner = false;
        // 是否是vip
        boolean isVip = false;

        for (SysUserRole sysUserRole : roleList) {
            if ("seniorPartner".equals(sysUserRole.getRoleCode())) {
                isSeniorPartner = true;
            }
            if ("partner".equals(sysUserRole.getRoleCode())) {
                isPartner = true;
            }
            if ("vip".equals(sysUserRole.getRoleCode())) {
                isVip = true;
            }
        }

        // 获取order的产品id
        Long productId = order.getProductId();
        // 根据产品id获取产品信息
        BusAcRechargeProduct busAcRechargeProduct = productRepository.findById(productId).orElseThrow(() ->
                BaseException.of("productNotFound", "产品不存在"));

        Double amount = null;

        if (isSeniorPartner) {
            // 高级合伙人的价格
            amount = busAcRechargeProduct.getSeniorPartnerPrice();
        } else if (isPartner) {
            // 合伙人的价格
            amount = busAcRechargeProduct.getPartnerPrice();
        } else if (isVip) {
            // vip的价格
            amount = busAcRechargeProduct.getVipUserPrice();
        } else {
            // 普通用户的价格
            amount = busAcRechargeProduct.getNormalUserPrice();
        }


        String description = "";
        if (busAcRechargeProduct.getTypeCode().equals("phone")) {
            description = "话费充值-" + order.getApiRechargeParam().getMobile() + "-" + busAcRechargeProduct.getShowPrice() + "元-" + busAcRechargeProduct.getTip();
        }

        FunctionalPayOrder functionalPayOrder = functionalPayOrderService.generatePayOrder(
                new FunctionalPayOrder()
                        .setUserId(self.getId())
                        .setProductCode("acRechargeProduct")
                        .setCurrency("CNY")
                        .setAmount(amount)
                        .setDescription(description)
        );

        Long orderId = functionalPayOrder.getId();
        BusAcRechargeProductOrder order1 = new BusAcRechargeProductOrder();
        order1.setOrderId(orderId);
        order1.setProductId(productId);
        order1.setUserId(self.getId());
        order1.setName(busAcRechargeProduct.getName());
        order1.setCostPrice(busAcRechargeProduct.getCostPrice());
        order1.setNormalUserPrice(busAcRechargeProduct.getNormalUserPrice());
        order1.setPartnerPrice(busAcRechargeProduct.getPartnerPrice());
        order1.setSeniorPartnerPrice(busAcRechargeProduct.getSeniorPartnerPrice());
        order1.setVipUserPrice(busAcRechargeProduct.getVipUserPrice());
        order1.setApiProductId(busAcRechargeProduct.getApiProductId());
        order1.setOrderStatusCode(FunctionalPayOrder.STATUS_CODE_UNPAID);
        order1.setShowPrice(busAcRechargeProduct.getShowPrice());
        order1.setTypeCode(busAcRechargeProduct.getTypeCode());
        order1.setTip(busAcRechargeProduct.getTip());
        orderRepository.save(order1);

        BusAcYrApiRechargeParam apiRechargeParam = order.getApiRechargeParam();
        apiRechargeParam.setProductId(busAcRechargeProduct.getApiProductId());
        apiRechargeParam.setOutTradeNum(orderId);
        apiRechargeParamRepository.save(apiRechargeParam);


        return functionalPayOrder;
    }
}
